package com.gitee.service.impl;

import com.gitee.constant.MessageConstant;
import com.gitee.context.BaseContext;
import com.gitee.entity.FansList;
import com.gitee.entity.FollowList;
import com.gitee.entity.User;
import com.gitee.exception.FollowListException;
import com.gitee.mapper.FansListMapper;
import com.gitee.mapper.FollowListMapper;
import com.gitee.mapper.UserMapper;
import com.gitee.service.FollowListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class FollowListServiceImpl implements FollowListService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private FollowListMapper followListMapper;
    @Resource
    private FansListMapper fansListMapper;

    @Override
    @Transactional
    public void follow(Integer followId) {
        // 关注的用户和当前登录用户id是同一个, 则异常
        // 不能关注自己
        Integer currentId = BaseContext.getCurrentId();
        if (followId.equals(currentId)) {
            throw new FollowListException(MessageConstant.CANT_FOLLOW_SELF);
        }

        // 查找当前用户的详情信息
        User currentUser = userMapper.selectById(currentId);
        // 通过要关注的id查找目标对象
        User followedUser = userMapper.selectById(followId);

        // 如果已经关注对方, 则警告
        // 查找followId绑定为本用户的数据
        // select * from sxq_follow_list where follow_id = #{followId} and user_id = #{userId}
        FollowList existFollow = followListMapper.selectByFollowIdAndUserId(followId, currentId);
        // 已经关注了, 这时候取消关注
        if (existFollow != null) {
            // 这里进行取消关注
            // 取消关注只需要用到id
            // 取消当前用户的关注列表
            // delete from follow_list where user_id = #{now} and follow_id = #{target}
            followListMapper.deleteByUserIdAndFollowId(currentId, followId);

            // 并删除目标用户的粉丝, 粉丝为当前用户
            // delete from fans_list where user_id = #{target} and fans_id = #{now}
            fansListMapper.deleteByUserIdAndFansId(followId, currentId);
        } else {
            // 这里进行关注操作
            // 准备数据
            FollowList followList = new FollowList();
            followList.setUserId(currentId);
            followList.setFollowId(followId);
            followList.setAvatar(followedUser.getAvatar());
            followList.setNickname(followedUser.getNickname());

            // 插入关注列表
            followListMapper.insert(followList);

            // 同时为被关注者新增粉丝
            FansList fansList = new FansList();
            fansList.setUserId(followId);
            fansList.setFansId(currentId);
            fansList.setNickname(currentUser.getNickname());
            fansList.setAvatar(currentUser.getAvatar());
            // 为被关注者新增粉丝
            fansListMapper.insert(fansList);
        }
        updateUserData(currentUser, followedUser);
    }

    /**
     * 更新用户表
     *
     * @param currentUser 当前用户
     * @param targetUser  目标用户
     */
    private void updateUserData(User currentUser, User targetUser) {
        // 先修改当前用户的
        // 更新关注数
        // 统计当前关注列表人数
        // 关注其他用户, 本用户只有关注列表变化, 粉丝列表不变化, 所以只更新关注列表
        Long count = followListMapper.count(currentUser.getId());
        // 更新当前用户的关注列表
        currentUser.setFollowCount(count);
        userMapper.updateById(currentUser);

        // 统计被关注者粉丝数量
        // 当关注其他用户后, 其他用户的粉丝数量变化, 所以只更新其他用户的粉丝列表
        count = fansListMapper.count(targetUser.getId());
        // 修改被关注者的粉丝列表
        targetUser.setFansCount(count);
        userMapper.updateById(targetUser);
    }

    @Override
    public List<FollowList> getFollow(Integer userId) {
        return followListMapper.selectByUserId(userId);
    }

    @Override
    public Boolean isFollow(Integer userId) {
        FollowList followList = followListMapper.selectByFollowIdAndUserId(userId, BaseContext.getCurrentId());
        return followList != null;
    }
}
